package com.seafood.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 冷链运输记录实体类
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "cold_chain_records")
public class ColdChainRecord extends BaseEntity {
    
    @NotBlank(message = "运输单号不能为空")
    @Column(name = "transport_number", unique = true, nullable = false, length = 100)
    private String transportNumber;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "order_id")
    private Order order;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", nullable = false)
    private Product product;
    
    @NotBlank(message = "运输公司不能为空")
    @Column(name = "transport_company", nullable = false, length = 200)
    private String transportCompany;
    
    @NotBlank(message = "车辆信息不能为空")
    @Column(name = "vehicle_info", nullable = false, length = 100)
    private String vehicleInfo;
    
    @NotBlank(message = "司机信息不能为空")
    @Column(name = "driver_info", nullable = false, length = 100)
    private String driverInfo;
    
    @Column(name = "origin_address", length = 500)
    private String originAddress;
    
    @Column(name = "destination_address", length = 500)
    private String destinationAddress;
    
    @NotNull(message = "计划温度范围不能为空")
    @DecimalMin(value = "-50.0", message = "最低温度不能低于-50°C")
    @DecimalMax(value = "50.0", message = "最高温度不能高于50°C")
    @Column(name = "planned_temp_min", precision = 5, scale = 2)
    private BigDecimal plannedTempMin;
    
    @NotNull(message = "计划温度范围不能为空")
    @DecimalMin(value = "-50.0", message = "最低温度不能低于-50°C")
    @DecimalMax(value = "50.0", message = "最高温度不能高于50°C")
    @Column(name = "planned_temp_max", precision = 5, scale = 2)
    private BigDecimal plannedTempMax;
    
    @Column(name = "planned_humidity_min", precision = 5, scale = 2)
    private BigDecimal plannedHumidityMin;
    
    @Column(name = "planned_humidity_max", precision = 5, scale = 2)
    private BigDecimal plannedHumidityMax;
    
    @Column(name = "start_time")
    private LocalDateTime startTime;
    
    @Column(name = "end_time")
    private LocalDateTime endTime;
    
    @Column(name = "estimated_duration_hours")
    private Integer estimatedDurationHours;
    
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private TransportStatus status = TransportStatus.PLANNED;
    
    @Column(name = "current_location", length = 500)
    private String currentLocation;
    
    @Column(name = "current_temperature", precision = 5, scale = 2)
    private BigDecimal currentTemperature;
    
    @Column(name = "current_humidity", precision = 5, scale = 2)
    private BigDecimal currentHumidity;
    
    @Column(name = "gps_coordinates", length = 100)
    private String gpsCoordinates;
    
    @Column(name = "alert_count")
    private Integer alertCount = 0;
    
    @Column(name = "quality_score", precision = 3, scale = 2)
    private BigDecimal qualityScore;
    
    @Lob
    private String notes;
    
    // 温度监控记录
    @OneToMany(mappedBy = "coldChainRecord", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<TemperatureLog> temperatureLogs;
    
    // 异常报警记录
    @OneToMany(mappedBy = "coldChainRecord", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<ColdChainAlert> alerts;
    
    /**
     * 运输状态枚举
     */
    public enum TransportStatus {
        PLANNED,        // 计划中
        IN_TRANSIT,     // 运输中
        DELIVERED,      // 已送达
        EXCEPTION,      // 异常
        CANCELLED       // 已取消
    }
}